<?php
// +----------------------------------------------------------------------
// | Fanwe 多语商城建站系统 (Build on ThinkPHP)
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 云淡风轻(97139915@qq.com)
// +----------------------------------------------------------------------

// 后台权限节点菜单
class CouponAction extends CommonAction{
	public function supplier_info()
	{
		$supplier_id = intval($_REQUEST['id']);
		$coupon_id = intval($_REQUEST['coupon_id']);
		$depart_list = M("SuppliersDepart")->where("supplier_id=".$supplier_id)->findAll();
		
		
		$coupon_info = M("Coupon")->getById($coupon_id);
		$depart_ids = $coupon_info['depart'];

		$depart_ids = explode(",",$depart_ids);
		
		foreach($depart_list as $k=>$v)
		{
			if(in_array($v['id'],$depart_ids))
			{
				$depart_list[$k]['checked'] = true;
			}
		}

		$this->assign("depart_list",$depart_list);
		
		$this->display();
	}
	public function city_info()
	{
		$city_id = intval($_REQUEST['id']);
		$coupon_id = intval($_REQUEST['coupon_id']);
		$region_list = M("CouponRegion")->where("city_id=".$city_id)->findAll();
		
		
		$coupon_info = M("Coupon")->getById($coupon_id);
		$region_id = intval($coupon_info['region_id']);
		
		foreach($region_list as $k=>$v)
		{
			if($v['id']==$region_id)
			{
				$region_list[$k]['selected'] = true;
			}
		}
		$this->assign("region_list",$region_list);
		
		$this->display();
	}
	//增
	public function add()
	{
		$city_list = D("GroupCity")->findAll();
		$this->assign("city_list",$city_list);
		
		$supplier_list = D("Suppliers")->findAll();
		$this->assign("supplier_list",$supplier_list);
		$this->display();
	}
	
	
	
	function insert() {
		//B('FilterString');
		$name=$this->getActionName();
		$model = D ($name);
		
		if (false === $data = $model->create ()) {
			$this->error ( $model->getError () );
		}
		
		
		if($data['name']=='')
		{
			$this->error("名称不能为空");
		}
		
		
		if(intval($data['region_id'])==0)
		{
			$this->error("请先为城市添加默认的商圈数据");
		}
		
		$data['depart'] = implode(",",$data['depart']);
		
		$data['end_time'] = empty($_REQUEST['end_time']) ? 0 : (localStrToTime($_REQUEST['end_time'])+(24*3600-1));
		$data['create_time'] = gmtTime();
		
		$upload_list = $this->uploadFile(0,"public");
		if($upload_list)
		{	

			foreach($upload_list as $upload_item)
			{
				if($upload_item['key']=="icon")
				{
					$data['icon'] = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="img")
				{
					$data['img'] = $upload_item['recpath'].$upload_item['savename'];
				}				
			}

		}
		
		$list=$model->add ($data);
		if ($list!==false) { //保存成功
			$this->saveLog(1,$list);
//			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('ADD_SUCCESS'));
		} else {
			//失败提示
			$this->saveLog(0,$list);
			$this->error (L('ADD_FAILED'));
		}
	}
	
	//改
	function edit() {
		$name=$this->getActionName();
		$model = M ( $name );
		$id = $_REQUEST [$model->getPk ()];
		$vo = $model->getById ( $id );
		$this->assign ( 'vo', $vo );		
		
		$city_list = D("GroupCity")->findAll();
		$this->assign("city_list",$city_list);
		
		$supplier_list = D("Suppliers")->findAll();
		$this->assign("supplier_list",$supplier_list);
		$this->display();
	}
	function update() {
		//B('FilterString');
		$name=$this->getActionName();
		$model = D ( $name );
		if (false === $data = $model->create ()) {
			$this->error ( $model->getError () );
		}
		$coupon = M("Coupon")->getById($data['id']);
				
		if($data['name']=='')
		{
			$this->error("名称不能为空");
		}
				
		if(intval($data['region_id'])==0)
		{
			$this->error("请先为城市添加默认的商圈数据");
		}
		
		$data['depart'] = implode(",",$data['depart']);

		$data['end_time'] = empty($_REQUEST['end_time']) ? 0 : (localStrToTime($_REQUEST['end_time'])+(24*3600-1));
		
		$upload_list = $this->uploadFile(0,"public");
		if($upload_list)
		{	
			foreach($upload_list as $upload_item)
			{
				if($upload_item['key']=="icon")
				{
					@unlink(getcwd().$coupon['icon']);
					$data['icon'] = $upload_item['recpath'].$upload_item['savename'];
				}
				if($upload_item['key']=="img")
				{
					@unlink(getcwd().$coupon['img']);
					$data['img'] = $upload_item['recpath'].$upload_item['savename'];
				}				
			}
		}
		
		$list=$model->save ($data);
		if (false !== $list) {
			//成功提示
			$this->saveLog(1);
//			$this->assign ( 'jumpUrl', Cookie::get ( '_currentUrl_' ) );
			$this->success (L('EDIT_SUCCESS'));
		} else {
			//错误提示
			$this->saveLog(0);
			$this->error (L('EDIT_FAILED'));
		}
	}
	
	
	public function foreverdelete() {
		//删除指定记录
		$name=$this->getActionName();
		$model = D ($name);
		if (! empty ( $model )) {
			$pk = $model->getPk ();
			$id = $_REQUEST [$pk];
			if (isset ( $id )) {
				$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
				if (false !== $model->where ( $condition )->delete ()) {
					//echo $model->getlastsql();
					$this->saveLog(1);
					$this->success (L('DEL_SUCCESS'));
				} else {
					$this->saveLog(0);
					$this->error (L('DEL_FAILED'));
				}
			} else {
				$this->saveLog(0);
				$this->error ( L('INVALID_OP'));
			}
		}
		$this->forward ();
	}
	
}
?>